package android.slc.module.component.attchment.domain;

import android.slc.box.core.property.SimpleWrapObjectProperty;
import android.slc.box.core.virtual_android.ActivityResultCallback;
import android.slc.box.mp.picker.entity.i.IBaseItem;
import android.slc.module.component.attchment.entity.Attachment;
import android.slc.module.component.attchment.utils.AttachmentUtils;

import java.util.ArrayList;
import java.util.List;

import android.slc.utils.entity.ValueWrap;
import cn.hutool.core.collection.CollUtil;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Function;
import javafx.beans.property.ReadOnlyObjectProperty;
import org.apache.commons.collections4.CollectionUtils;

/**
 * 附件view model
 *
 * @author slc
 * @date 2020-09-08 11:09
 */
public class AttachmentEditVmBox<T extends AttachmentReviewVmBox.AttachmentAdapter> extends AttachmentReviewVmBox<T> {
    public final ActivityResultCallback<List<IBaseItem>> selectAttEventResultCallback = this::onSelectAttResultCallback;
    protected final SimpleWrapObjectProperty<Void> selectAttEvent = new SimpleWrapObjectProperty<>();
    public final SimpleWrapObjectProperty<Boolean> showAddBtn = new SimpleWrapObjectProperty<>(true);
    private int maxCount = 9;

    public AttachmentEditVmBox() {
    }

    protected void onDataListChangedCallback(List<Attachment> dataList) {
        super.onDataListChangedCallback(dataList);
        showAddBtn.setValuePlus(dataList.size() < maxCount);
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    public int getMaxCount() {
        return maxCount;
    }

    public ReadOnlyObjectProperty<ValueWrap<Void>> getSelectAttEvent() {
        return selectAttEvent;
    }

    /**
     * 选择附件
     */
    public void selectAtt() {
        selectAttEvent.notifyChange();
    }

    /**
     * 上传文件
     */
    public Observable<Attachment> uploadAttachment() {
        return getAttachmentAdapter().uploadAttachment();
    }

    /**
     * 添加附件监听
     *
     * @param iBaseItems
     */
    protected void onSelectAttResultCallback(List<IBaseItem> iBaseItems) {
        if (iBaseItems != null) {
            List<Attachment> attachmentList = new ArrayList<>();
            for (IBaseItem iBaseItem : iBaseItems) {
                attachmentList.add(new Attachment(iBaseItem));
            }
            if (!CollectionUtils.isEmpty(attachmentList)) {
                CollUtil.forEach(attachmentList,(item, index) -> {
                    item.setIsAllowEditStatus(AttachmentUtils.IS_ALLOW_EDIT_STATUS_AGREE);
                });
                getDataList().addAll(attachmentList);
                dataListOf.notifyChange();
            }
        }
    }

    @Override
    public void onSucceed(List<Attachment> data) {
        CollUtil.forEach(data,(item, index) -> {
            item.setIsAllowEditStatus(AttachmentUtils.IS_ALLOW_EDIT_STATUS_AGREE);
        });
        super.onSucceed(data);
    }

    public Function<Attachment, Attachment> refreshAttachment() {
        return attachment -> {
            notifyAttItemDataChangeLd.setValuePlus(attachment);
            return attachment;
        };
    }

}
